//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.IO;
using System.Web;
using System.Xml;
using CommunityServer.Components;

namespace CommunityServer.Blogs.Components
{
	/// <summary>
	/// Summary description for OpmlHandler.
	/// </summary>
	public class OpmlHandler : IHttpHandler
	{
        protected const string styleElement = "<?xml-stylesheet type=\"text/xsl\" href=\"{0}\" media=\"screen\"?>";

		public OpmlHandler()
		{
		}

        public void ProcessRequest(HttpContext context)
        {
			int groupID = Globals.SafeInt(context.Request.QueryString["GroupID"], -1);
			BlogUrls urls = BlogUrls.Instance();
			string baseUrl = Globals.HostPath(context.Request.Url);
            string _styleSheet = Globals.GetSiteUrls().OpmlStyleSheet();

			context.Response.ContentEncoding = System.Text.Encoding.UTF8;
			context.Response.ContentType = "text/xml";
			context.Response.Cache.SetCacheability(HttpCacheability.Public);
			context.Response.Cache.VaryByParams["GroupID"] = true;
			context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(5));
			context.Response.Cache.SetValidUntilExpires(true);

			XmlTextWriter writer = new XmlTextWriter(context.Response.Output);
			writer.Formatting = Formatting.Indented;

            // XSL Style Sheet Element
            context.Response.Write(string.Format(styleElement, string.Concat(baseUrl, _styleSheet)));

			// OPML Element
			writer.WriteStartElement("opml");
			writer.WriteAttributeString("version", "2.0");

			// Head Element
			writer.WriteStartElement("head");
			writer.WriteElementString("title", CSContext.Current.SiteSettings.SiteName);
			writer.WriteElementString("dateCreated", DateTime.Now.ToUniversalTime().ToString("r"));
			writer.WriteEndElement();

			// Body Element
			writer.WriteStartElement("body");

			// Write outline elements
			if(groupID > -1)
			{
				Group group = WeblogGroups.GetWeblogGroup(groupID, true, false);
				if (group != null)
					WriteGroup(writer, group, urls, baseUrl);
			}
			else
			{
				ArrayList groups = WeblogGroups.GetWeblogGroups(true, false, false);
				foreach(Group group in groups)
				{
					WriteGroup(writer, group, urls, baseUrl);
				}
			}

			writer.WriteEndElement();
			writer.WriteEndElement();
			writer.Flush();
			writer.Close();
            
        }

        protected void WriteGroup(XmlTextWriter writer, Group group, BlogUrls urls, string baseUrl)
        {
			writer.WriteStartElement("outline");
			writer.WriteAttributeString("text", group.Name);

			ArrayList weblogs = Weblogs.GetWeblogsByGroupID(group.GroupID, true, false, false);
			foreach(Weblog blog in weblogs)
			{
				writer.WriteStartElement("outline");
				writer.WriteAttributeString("type", "rss");
				writer.WriteAttributeString("text", blog.Name);
				writer.WriteAttributeString("title", blog.Name);
				writer.WriteAttributeString("description", blog.Description);
				writer.WriteAttributeString("xmlUrl", baseUrl + urls.Rss(blog.ApplicationKey));
				writer.WriteAttributeString("htmlUrl", baseUrl + urls.HomePage(blog.ApplicationKey));
				writer.WriteEndElement();
			}

			writer.WriteEndElement();
        }

	    public bool IsReusable
	    {
	        get { return false; }
	    }

	}
}
